package com.homework.controller;

import cn.hutool.crypto.digest.MD5;
import com.homework.dto.LoginDto;
import com.homework.dto.RegisterDto;
import com.homework.entity.Admin;
import com.homework.entity.Student;
import com.homework.entity.Teacher;
import com.homework.pojo.Result;
import com.homework.service.*;
import com.homework.utils.JwtUtil;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AssignmentService assignmentService;
    @Autowired
    private StudentAssignmentService studentAssignmentService;
    @Autowired
    private StudentTeacherService studentTeacherService;


    @PostMapping("login")
    public Result login(@RequestBody @Valid LoginDto loginDto) {
        String password = MD5.create().digestHex16(loginDto.getPassword());
        Object one = null;
        HashMap<String, Object> hashMap = new HashMap<>();

        if (loginDto.getRole().equals("0")) {
            one = studentService.lambdaQuery()
                    .eq(Student::getStudentNo, loginDto.getUserId())
                    .eq(Student::getPassword, password)
                    .one();
            if (one == null)
                return Result.error("用户名或密码错误");
            Student student = (Student) one;
            hashMap.put("userId", student.getStudentId());
        } else if (loginDto.getRole().equals("1")) {
            one = teacherService.lambdaQuery()
                    .eq(Teacher::getTeacherNo, loginDto.getUserId())
                    .eq(Teacher::getPassword, password)
                    .one();
            if (one == null)
                return Result.error("用户名或密码错误");
            Teacher teacher = (Teacher) one;
            hashMap.put("userId", teacher.getTeacherId());
        } else {
            one = adminService.lambdaQuery()
                    .eq(Admin::getUsername, loginDto.getUserId())
                    .eq(Admin::getPassword, password)
                    .one();
            if (one == null)
                return Result.error("用户名或密码错误");
            hashMap.put("userId", ((Admin) one).getAdminId());
        }
        hashMap.put("role", loginDto.getRole());
        String token = JwtUtil.createToken(hashMap);
        return Result.success(token);
    }

    @PostMapping("register")
    public Result register(@RequestBody @Valid RegisterDto registerDto) {
        Object one = null;
        String password = MD5.create().digestHex16(registerDto.getPassword());
        if (registerDto.getConfirmPassword() != null && !registerDto.getConfirmPassword().equals(registerDto.getPassword()))
            return Result.error("两次密码不一致");
        if (registerDto.getRole().equals("0")) {
            one = studentService.lambdaQuery()
                    .eq(Student::getStudentId, registerDto.getUserId())
                    .one();
            if (one != null)
                return Result.error("用户已存在!");
            Student student = new Student();
            BeanUtils.copyProperties(registerDto, student);
            student.setStudentNo(String.valueOf(registerDto.getUserId()));
            student.setPassword(password);
            studentService.save(student);
            return Result.success("注册成功");

        } else if (registerDto.getRole().equals("1")) {
            one = teacherService.lambdaQuery()
                    .eq(Teacher::getTeacherId, registerDto.getUserId())
                    .one();
            if (one != null)
                return Result.error("用户已存在!");
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(registerDto, teacher);
            teacher.setTeacherNo(String.valueOf(registerDto.getUserId()));
            teacher.setPassword(password);
            teacherService.save(teacher);
            return Result.success("注册成功");
        }
        return Result.error("不要乱来！");
    }
}
